import { http } from "@/utils/http";

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type commonResult<T> = {
  code: number;
  msg: string;
  result: T;
};

export type UserInfo = {
  permissions: any[];
  userInfo: {
    id: number;
    nickname: string;
    phone: string;
    lastLoginTime: string;
    lastLoginIP: string;
    roleId: number;
    roleName: string;
    openid:string
    loginName: string;
  };
};
export type getCaptchaResult = {
  captchaId: string;
  captchaImg: string;
  captchaType: number;
};
export type getPhoneCodeResult = {};
export type LoginResult = {
  accessToken: string;
  sessionKey?: string;
};
/** 1.2.1.1 获取验证码 */
export const getCaptcha = () => {
  return http.request<commonResult<getCaptchaResult>>(
    "get",
    "/system/system/v2/user/captcha"
  );
};
/** 1.2.1.2 退出登录 */
export const outLogin = (data?: object) => {
  return http.request<commonResult<{}>>(
    "post",
    "/system/system/v2/user/logout",
    { data }
  );
};
/** 1.2.1.3 登陆-密码登录 */
export const getLogin = (data?: object) => {
  return http.request<commonResult<LoginResult>>(
    "post",
    "/system/system/v2/user/login",
    { data }
  );
};
/** 1.2.1.4 获取个人信息 */
export const getSelfInfo = () => {
  return http.request<commonResult<UserInfo>>(
    "get",
    "/system/system/v2/user/self"
  );
};
/** 1.2.1.5 修改密码 */
export const editPassword = (data?: object) => {
  return http.request<commonResult<{}>>(
    "put",
    "/system/system/v2/user/password",
    { data }
  );
};
/** 1.2.1.6 登录(手机验证码登录) */
export const loginPhone = (data?: object) => {
  return http.request<commonResult<LoginResult>>(
    "post",
    "/system/system/v2/user/login-phone",
    { data }
  );
};
/** 1.2.1.7 发送验证码 */
export const getPhoneCode = (data?: object) => {
  return http.request<commonResult<{}>>(
    "post",
    "/system/system/v2/user/code",
    { data }
  );
};
/** 1.2.1.8修改个人信息 */
export const editUserInfo = (data?: object) => {
  return http.request<commonResult<{}>>("put", "/system/system/v2/user/self", {
    data
  });
};
/** 1.2.1.9 新注册单位注册 */
export const registerUnit = (data?: object) => {
  return http.request<commonResult<{}>>(
    "post",
    "/system/system/v2/unity-register/register",
    { data }
  );
};







/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refresh-token", { data });
};

/** 1.2.4.2 删除图片 */
export const removeFileApi = (data?: object) => {
  return http.request<commonResult<{}>>(
    "delete",
    "/system/system/v2/file/file",
    { data }
  );
};
/** 1.2.4.1 上传图片 */
export const uploadFile = (data?: object) => {
  return http.request<commonResult<{}>>(
    "post",
    "/system/system/v2/file/upload",
    { data }
  );
};
/** 1.2.1.10 获取登录协议和系统名称 */
export const getLoginAgreement = () => {
  return http.request<commonResult<any>>(
    "get",
    "/system/system/v2/user/protocol"
  );
};

/** 1.2.1.12 获取用户在所选单位下的权限 */
export const getUserRole = () => {
  return http.request<commonResult<any>>(
    "get",
    "/system/system/v2/user/unity-perms"
  );
};
